#include "main.h"


/**
  * @brief  main Function
  */
int main(void)
{
	System_InitConfig();
	LPTIM32_Control(LPTIM32_RUNSTART);
	DEBUG_Printf(0, "This is 4G Module test code.");
    EG912U_PowerControlIO_InitConfig();
	RS0102_EnableIO_InitConfig();
	EG912U_PWOER_ON();
	RS0102_Enable();
	while(1){
        if(COM4.RxBuffHead != COM4.RxBuffTail)
        {
            FL_UART_WriteTXBuff(UART0, COM4.RxBuff[COM4.RxBuffHead]);
            COM4.RxBuffHead = (COM4.RxBuffHead + 1) % USART4_BUFFMAXSIZE;
        }
        Key_Task();
        RunIndicate_Task();
	}
}


void System_InitConfig(void)
{
	SEGGER_RTT_Init();
	LED_InitConfig(); 
	Key_InitConfig();
	LPTIM32_InitConfig();
	LPTIM32_NVIC_InitConfig();
    USART0_InitConfig(115200);
    USART0_ReceiveIntertupt_InitConfig();
    USART4_InitConfig(115200);
    USART4_ReceiveIntertupt_InitConfig();
}

void EG912U_PowerControlIO_InitConfig(void)
{
	FL_GPIO_InitTypeDef	GPIO_InitStruct = {0};

	GPIO_InitStruct.pin = FL_GPIO_PIN_14;
	GPIO_InitStruct.mode = FL_GPIO_MODE_OUTPUT;
	GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
	GPIO_InitStruct.pull = FL_DISABLE;
	GPIO_InitStruct.remapPin = FL_DISABLE;

	FL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

void RS0102_EnableIO_InitConfig(void)
{
	FL_GPIO_InitTypeDef	GPIO_InitStruct = {0};

	GPIO_InitStruct.pin = FL_GPIO_PIN_13;
	GPIO_InitStruct.mode = FL_GPIO_MODE_OUTPUT;
	GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
	GPIO_InitStruct.pull = FL_DISABLE;
	GPIO_InitStruct.remapPin = FL_DISABLE;

	FL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

void Key_Task(void)
{
    static uint32_t previous_tick = 0;
    if(CalculateInterval(LPTIM32_Info.MsCount, previous_tick) < KEY_TASK_PERIOD){return;}
    previous_tick = LPTIM32_Info.MsCount;
    button_ticks();
}

void RunIndicate_Task(void)
{
    static uint32_t previous_tick = 0;
    if(CalculateInterval(LPTIM32_Info.MsCount, previous_tick) < RUNINDICATE_TASK_PERIOD){return;}
    previous_tick = LPTIM32_Info.MsCount;
    FL_GPIO_ToggleOutputPin(GPIOB, FL_GPIO_PIN_7);
}
